package com.milhouz.wormdroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.milhouz.wormdroid.util.ProfileRecorder;

public class EntryActivity extends Activity {
	private static final int ACTIVITY_TEST = 0;
	private static final int RESULTS_DIALOG = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		// Sets up a click listener for the Run Test button.
		Button button;
		button = (Button) findViewById(R.id.runTest);
		button.setOnClickListener(mRunTestListener);

		// Turns on one item by default in our radio groups--as it should be!
		/*RadioGroup group = (RadioGroup) findViewById(R.id.renderMethod);
		group.setOnCheckedChangeListener(mMethodChangedListener);
		group.check(R.id.methodCanvas);

		RadioGroup glSettings = (RadioGroup) findViewById(R.id.GLSettings);
		glSettings.check(R.id.settingVerts);*/

	}

	/** Passes preferences about the test via its intent. */
	protected void initializeIntent(Intent i) {
		//final CheckBox checkBox = (CheckBox) findViewById(R.id.animateSprites);
		//final boolean animate = checkBox.isChecked();
		
		// -- Get team count
		final EditText teamsEditText = (EditText) findViewById(R.id.teamCount);
		final int teamCount = Integer.parseInt(teamsEditText.getText().toString());
		// -- Get bot count
		final EditText botsEditText = (EditText) findViewById(R.id.spriteCount);
		final int botCount = Integer.parseInt(botsEditText.getText().toString());
		// -- Get team count
		final EditText timeEditText = (EditText) findViewById(R.id.roundTime);
		final int roundTime = Integer.parseInt(timeEditText.getText().toString());
		
		i.putExtra("teamCount", teamCount);		
		i.putExtra("spriteCount", botCount);
		i.putExtra("roundTime", roundTime);
	}

	/**
	 * Responds to a click on the Run Test button by launching a new test activity.
	 */
	View.OnClickListener mRunTestListener = new OnClickListener() {
		public void onClick(View v) {
			//RadioGroup group = (RadioGroup) findViewById(R.id.renderMethod);
			Intent i;
			i = new Intent(v.getContext(), GameActivity.class);
			//RadioGroup glSettings = (RadioGroup) findViewById(R.id.GLSettings);
			//if (glSettings.getCheckedRadioButtonId() == R.id.settingVerts) {
			//	i.putExtra("useVerts", true);
			//} else if (glSettings.getCheckedRadioButtonId() == R.id.settingVBO) {
			//	i.putExtra("useVerts", true);
			//	i.putExtra("useHardwareBuffers", true);
			//}
			initializeIntent(i);
			startActivityForResult(i, ACTIVITY_TEST);
		}
	};

	/**
	 * Enables or disables OpenGL ES-specific settings controls when the render method option changes.
	 */
	RadioGroup.OnCheckedChangeListener mMethodChangedListener = new RadioGroup.OnCheckedChangeListener() {
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			/*if (checkedId == R.id.methodCanvas) {
				findViewById(R.id.settingDrawTexture).setEnabled(false);
				findViewById(R.id.settingVerts).setEnabled(false);
				findViewById(R.id.settingVBO).setEnabled(false);
			} else {
				findViewById(R.id.settingDrawTexture).setEnabled(true);
				findViewById(R.id.settingVerts).setEnabled(true);
				findViewById(R.id.settingVBO).setEnabled(true);
			}*/
		}
	};

	/** Creates the test results dialog and fills in a dummy message. */
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		if (id == RESULTS_DIALOG) {

			String dummy = "No results yet.";
			CharSequence sequence = dummy.subSequence(0, dummy.length() - 1);
			dialog = new AlertDialog.Builder(this).setTitle(R.string.dialog_title).setPositiveButton(R.string.dialog_ok, null).setMessage(sequence).create();
		}
		return dialog;
	}

	/**
	 * Replaces the dummy message in the test results dialog with a string that describes the actual test results.
	 */
	protected void onPrepareDialog(int id, Dialog dialog) {
		if (id == RESULTS_DIALOG) {
			// Extract final timing information from the profiler.
			final ProfileRecorder profiler = ProfileRecorder.sSingleton;
			final long frameTime = profiler.getAverageTime(ProfileRecorder.PROFILE_FRAME);
			final long frameMin = profiler.getMinTime(ProfileRecorder.PROFILE_FRAME);
			final long frameMax = profiler.getMaxTime(ProfileRecorder.PROFILE_FRAME);

			final long drawTime = profiler.getAverageTime(ProfileRecorder.PROFILE_DRAW);
			final long drawMin = profiler.getMinTime(ProfileRecorder.PROFILE_DRAW);
			final long drawMax = profiler.getMaxTime(ProfileRecorder.PROFILE_DRAW);

			final long flipTime = profiler.getAverageTime(ProfileRecorder.PROFILE_PAGE_FLIP);
			final long flipMin = profiler.getMinTime(ProfileRecorder.PROFILE_PAGE_FLIP);
			final long flipMax = profiler.getMaxTime(ProfileRecorder.PROFILE_PAGE_FLIP);

			final long simTime = profiler.getAverageTime(ProfileRecorder.PROFILE_SIM);
			final long simMin = profiler.getMinTime(ProfileRecorder.PROFILE_SIM);
			final long simMax = profiler.getMaxTime(ProfileRecorder.PROFILE_SIM);

			final float fps = frameTime > 0 ? 1000.0f / frameTime : 0.0f;

			String result = "Frame: " + frameTime + "ms (" + fps + " fps)\n" + "\t\tMin: " + frameMin + "ms\t\tMax: " + frameMax + "\n" + "Draw: " + drawTime + "ms\n" + "\t\tMin: " + drawMin + "ms\t\tMax: " + drawMax + "\n" + "Page Flip: " + flipTime + "ms\n" + "\t\tMin: " + flipMin + "ms\t\tMax: " + flipMax + "\n" + "Sim: " + simTime + "ms\n" + "\t\tMin: " + simMin + "ms\t\tMax: " + simMax + "\n";
			CharSequence sequence = result.subSequence(0, result.length() - 1);
			AlertDialog alertDialog = (AlertDialog) dialog;
			alertDialog.setMessage(sequence);
		}
	}

	/** Shows the results dialog when the test activity closes. */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		showDialog(RESULTS_DIALOG);

	}
}